/*
 * Copyright (C) 2021 The Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.oushangfeng.pinneddemo.custom.categorylist;

import com.oushangfeng.pinneddemo.ResourceTable;
import com.oushangfeng.pinneddemo.datainterface.CategoryItemBase;
import com.oushangfeng.pinnedsectionitemdecoration.callback.datainterface.Category;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.app.Context;

import java.util.List;

/**
 * CommonCategory
 *
 * @since 2021-03-27
 * Created by oubowu on 2021/3/27 10:29
 */
public class CommonCategory implements Category {
    private static final float CORNER_RADIUS = 0;
    private final Context context;
    private final List<CategoryItemBase> categoryItems;
    private final int categoryId;

    /**
     * CommonCategory
     *
     * @param context
     * @param categoryItems
     * @param id
     */
    public CommonCategory(Context context, List<CategoryItemBase> categoryItems, int id) {
        this.context = context;
        this.categoryItems = categoryItems;
        this.categoryId = id;
    }

    private void setComponentShapeAndBackground(Component component, int index) {
        // Set corners and background
        ComponentContainer.LayoutConfig layoutConfig = component.getLayoutConfig();
        Element element = component.getBackgroundElement();
        if (!(element instanceof ShapeElement)) {
            return;
        }

        ShapeElement shapeElement = (ShapeElement) element;
        boolean isCategoryFirstItem = hasHead() ? (index == 1) : (index == 0);
        boolean isCategoryLastItem = index == (categoryItems.size() - 1);
        if (isCategoryFirstItem && isCategoryLastItem) {
            // only one item in category, set all corner radius and bottom margin 12vp
            shapeElement.setCornerRadiiArray(new float[]{CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS,
                CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS});
            Component componentItemDivider = component.findComponentById(ResourceTable.Id_item_divider);
            componentItemDivider.setVisibility(Component.HIDE);
        } else if (isCategoryFirstItem) {
            // first item in category, set top two corners radius
            shapeElement.setCornerRadiiArray(
                    new float[]{CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS, 0, 0, 0, 0});
        } else if (isCategoryLastItem) {
            // last Item in category, set down two corners radius and bottom margin 12vp
            shapeElement.setCornerRadiiArray(
                    new float[]{0, 0, 0, 0, CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS});
            Component componentItemDivider = component.findComponentById(ResourceTable.Id_item_divider);
            componentItemDivider.setVisibility(Component.HIDE);
        } else {
            // middle item in category, set no corners radius and show divider
            shapeElement.setCornerRadiiArray(new float[]{0, 0, 0, 0, 0, 0, 0, 0});
            Component componentItemDivider = component.findComponentById(ResourceTable.Id_item_divider);
            componentItemDivider.setVisibility(Component.VISIBLE);
        }

        component.setLayoutConfig(layoutConfig);
        component.setBackground(shapeElement);
    }

    /**
     * Get total category items count
     *
     * @return count of items
     */
    @Override
    public int getCategoryItemsCount() {
        return categoryItems.size();
    }

    /**
     * Get the type of category item in specified index
     *
     * @param index category item index
     * @return category type
     */
    @Override
    public int getCategoryItemType(int index) {
        return categoryItems.get(index).getItemType();
    }

    @Override
    public boolean hasHead() {
        return categoryItems.get(0).getItemType() == HeadItem.ITEM_TYPE;
    }

    @Override
    public Component createComponent(int index) {
        CategoryItemBase categoryItem = categoryItems.get(index);
        return categoryItem.createComponent();
    }

    @Override
    public void bindComponent(Component component, int index) {
        CategoryItemBase categoryItem = categoryItems.get(index);
        categoryItem.bindComponent(component);
        if (categoryItem.getItemType() == SearchItem.ITEM_TYPE) {
            return;
        }
        if (categoryItem.getItemType() != HeadItem.ITEM_TYPE) {
            setComponentShapeAndBackground(component, index);
        }
    }

    @Override
    public String getItemVaule(int index) {
        return categoryItems.get(index).getItem();
    }

    @Override
    public void setItemVaule(int index, String value) {
        categoryItems.get(index).setItem(value);
    }

    @Override
    public String getItemName(int i) {
        return categoryItems.get(i).getItemName();
    }
}
